package com.gzw.dms.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.widget.TosGallery;

import java.util.HashMap;
import java.util.Map;

public class WebViewActivity extends BaseActivity {

    private WebView webView;
    private Intent mIntent;
    private Toolbar toolbar;
    private ProgressBar progressbar;
    private Context mContext;
    private ProgressBar pg1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_web_view);


    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        webView = (WebView) findViewById(R.id.webView);
        pg1=(ProgressBar) findViewById(R.id.progressBar1);


    }

    @Override
    protected void initData() {
        mIntent = getIntent();
        mContext = this;

        String order_id = (String) mIntent.getExtras().get(
                "shippingOrder");
        String sate = (String)mIntent.getExtras().get("state");
        if (sate.equals("3")){
            toolbar.setTitle("异常图片");
        }else {
            toolbar.setTitle("签收图片");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("type", sate);
        map.put("order_id",order_id );
        String json = JsonUtil.toJson(map);
//        String url = "http://192.168.5.43:8080/scms/app.do?method=appToOrderImagesJsp&json="+json;
        String url = "http://139.129.216.151:8088/scms/app.do?method=appToOrderImagesJsp&json="+json;
//        String url = "http://139.129.216.151:56788/scms/app.do?method=appToOrderImagesJsp&json="+json;

//        //支持javascript
//        webView.getSettings().setJavaScriptEnabled(true);
//        // 设置可以支持缩放
//        webView.getSettings().setSupportZoom(true);
//        // 设置出现缩放工具
//        webView.getSettings().setBuiltInZoomControls(true);
//        //扩大比例的缩放
//        webView.getSettings().setUseWideViewPort(true);
//        //自适应屏幕
//        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//        webView.getSettings().setLoadWithOverviewMode(true);

        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });


        WebSettings seting=webView.getSettings();
        seting.setJavaScriptEnabled(true);//设置webview支持javascript脚本
        seting.setBuiltInZoomControls(true); // 显示放大缩小 controler
        seting.setSupportZoom(true); // 可以缩放
        seting.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);// 默认缩放模式
        seting.setUseWideViewPort(true);
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO 自动生成的方法存根

                if(newProgress==100){
                    pg1.setVisibility(View.GONE);//加载完网页进度条消失
                }
                else{
                    pg1.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    pg1.setProgress(newProgress);//设置进度值
                }

            }
        });


    }


    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new mOnClick());

    }

    class mOnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {

                default:
                    finish();
                    break;
            }
        }
    }


}
